package com.lz.ovuola.web.mvc;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.lz.ovuola.util.exception.BusinessException;
import com.lz.ovuola.util.exception.msg.CommonMsgEnum;
import com.lz.ovuola.util.response.ResponseDataPackageUtils;

@RestControllerAdvice
public class GlobalExceptionHandler {

	private Logger logger = LoggerFactory.getLogger("GlobalExceptionHandler");

	/**
	 * json响应
	 * 
	 * @param req
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(value = BusinessException.class)
	public String businessExceptionHandler(HttpServletRequest req,
			BusinessException ex) {
		logger.error("---host {},code:{} errorMsg: {}", req.getRemoteHost(),
				ex.getStatus(), ex.getMsg());
		return ResponseDataPackageUtils.fail(ex.getStatus(), ex.getMsg());
	}

	/**
	 * 所有底层异常
	 * 
	 * @param req
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(value = Exception.class)
	public String baseErrorHandler(HttpServletRequest req, Exception ex) {
		logger.error("---host {} ERROR: {}", req.getRemoteHost(),
				ex.getMessage());
		return ResponseDataPackageUtils.fail(
				CommonMsgEnum.SERVER_INTERNAL_ERROR.getStatus(),
				CommonMsgEnum.SERVER_INTERNAL_ERROR.getMsg());
	}
}